package ca.ualberta.cs.CLRServer.resources;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import uk.ac.liv.c3connector.AnnotationModel;
import ca.ualberta.cs.CLRDB.User;
import ca.ualberta.cs.CLRServer.Interface.SecureResource;
import ca.ualberta.cs.CLRServer.Interface.ServerInterface;

@Path("/publishService/users")
public class UsersController extends SecureResource{

	
	@Path("/auth")
	@POST 
    @Consumes("application/json")
    @Produces("application/json")
    public List<JAXBBean> authenticate(List<JAXBBean> requestArr){
    	
		checkBufferOverflow(requestArr,3); //2 fields, but all requests come in a size of 3X
		checkRequiredKeys(requestArr, "username","password");
//		System.out.println("auth called::***************");
		
    	String username = "";
    	String pass = "";
    	
    	for(int i = 0 ; i < requestArr.size() ; i++ ){
    		if( requestArr.get(i).getKey().equals("password") )
    			pass = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("username") )
    			username = requestArr.get(i).getVal();
    	}
    	    	
    	HashMap<String,String> userInfo = ServerInterface.db.getPersonsManagement().authenticate(username, pass);
//    	System.out.println("after db's response.");
    	List<JAXBBean> ret = new ArrayList<JAXBBean>();
    	for(String key : userInfo.keySet() ){
    		JAXBBean bean = new JAXBBean();
    		bean.setKey(key);
    		bean.setVal(userInfo.get(key));
    		ret.add(bean);
    	}
    		 	
    	return ret;
    	
    }
	
	/**
	 * Handles the request to add a new user
	 * @param requestArr (List of JAXBBean)
	 * @return status code: 0: ok, return 1: username exists, return 2: email already taken, 3: exception
	 */
	@Path("/add")
	@POST 
    @Consumes("application/json")
    //@Produces("text/plain")
    public String addUser(List<JAXBBean> requestArr){
    	    	
		checkBufferOverflow(requestArr,6);
		checkRequiredKeys(requestArr, "username","password","name","affiliation","description","email");
		
//		System.out.println("add user called in server.........");
		/*User user = new User();
		for(int i = 0 ; i < requestArr.size() ; i++ ){ //each key in reqArr is a field name of the User.java
			try {
				if(requestArr.get(i).getKey().equals("password"))
					user.setPassword(requestArr.get(i).getVal());
				else
					user.getClass().getDeclaredField(requestArr.get(i).getKey()).set(user, requestArr.get(i).getVal());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {				
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}	*/
		String username = null, pass = null, 
				name = null, aff = null, 
				desc = null, email = null;
		
		for(int i = 0 ; i < requestArr.size() ; i++ ){
    		if( requestArr.get(i).getKey().equals("password") )
    			pass = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("username") )
    			username = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("affiliation"))
    			aff = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("description"))
    			desc = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("name"))
    			name = requestArr.get(i).getVal();
    		else if( requestArr.get(i).getKey().equals("email"))
    			email = requestArr.get(i).getVal();
    	}
		User user = new User(username, pass, name, email, aff, desc);
    	    	
		System.out.println("\n--------------------Add User---------------------");
		System.out.println("add user: "+user.getUsername());
		System.out.println("-----------------------------------------");
		System.out.println();
		
    	return String.valueOf(ServerInterface.db.getPersonsManagement().addUser(user));    	
    }
}
